import { openwin } from './tool.js';
import $util from './util.js'
const $shop = {
	openshop(ly = '',id=0){
	if(ly == '' || id == 0){
		$util.alert('此商品失效');
		return
	}
	
	//自营
	if(ly == 4){
		openwin('/pages/shop/shopdetail/shopdetail?id='+id);
		return
	}
	if(ly == 1){
	this.jingdong(id);
	}else if(ly == 2 || ly == 3){
	this.tianmao(id);	
	}else if(ly == 5){
	this.pingduoduo(d);		
	}	
	},
	//京东
	jingdong(id){
	$util.showProgress()
	$util.ajax({
		url:'Shopother/JdZm_wx',
		link:id
	},(res,err)=>{
	$util.hideProgress()
	if(res.code == 1){
	console.log(res)
	setTimeout(()=>{
	getApp().globalData.clicklog.push('jingdong?id='+id);	
	},1000)
	// #ifdef H5
	$util.openwin(res.data.shortURL)
	// #endif
	
	// #ifdef MP
	wx.navigateToMiniProgram({
	  appId: 'wx7861f5e804222332',
	  shortLink: res.data.weChatShortLink,
	  success(res) {
	    // 打开成功
	  }
	})
	// #endif
	
	// #ifdef APP
	const isApp = plus.runtime.isApplicationExist({
	        pname: 'com.jingdong.app.mall'
	    });
	if(!isApp){
		$util.openwin(res.data.shortURL)
		return
	}
	const jdlm = uni.requireNativePlugin('LYKJ-JDLMSDK');
	jdlm.openByUrl({'url':res.data.shortURL})
	//$util.openwin(res.data.shortURL)
	// #endif
	}else{
	$util.toast(res.msg)
	}
	})	
	},
	//淘宝、天猫
	tianmao(id){
	$util.showProgress()
	$util.ajax({
		url:'Shopother/TmZm',
		link:id
	},(res,err)=>{
	$util.hideProgress()
	if(res.code == 1){
		setTimeout(()=>{
		getApp().globalData.clicklog.push('taobao?id='+id);	
		},1000)
	console.log(res) 
	// #ifdef APP
	const isApp = plus.runtime.isApplicationExist({
	        pname: 'com.taobao.taobao'
	    });
	
	if(!isApp){
		$util.openwin(res.data.shortUrl)
		return
	}
	let bc = uni.requireNativePlugin("FFX-Alibc");
	bc.openByUrl({url:res.data.shortUrl},(e)=>{
	        console.log(e);
	        if(e.code == 0){
			openwin(res.data.shortUrl)	
			}
	    });
	// #endif
	
	// #ifdef H5
	openwin(res.data.shortUrl)
	// #endif
	
	// #ifdef MP
	uni.setClipboardData({
		data: res.data.longTpwd,
		success: function () {
			$util.alert('已复制淘口令并送券！打开淘宝APP将跳转商品！')
		}
	});
	// #endif
	
	}else{
	$util.toast(res.msg)
	}
	})
	
	
	// #ifndef APP
	
	// #endif
	}
}
export default $shop;